using Clancy.Interfaces;
using System;
using System.ComponentModel;
using System.Drawing;
using System.Xml;
using System.Xml.Serialization;

namespace Clancy.Data
{
    /// <summary>
    ///
    /// </summary>
    public class SurveilanceObject : BaseDataContainer, ISurveilanceObject
    {
        /// <summary>
        ///
        /// </summary>
        [XmlIgnore]
        [DisplayName("Alarm delay time")]
        [Category("Detection && Record")]
        [Description("Time to record after the event has finished.")]
        public TimeSpan AlarmDelayTime
        {
            get { return (TimeSpan)GetProperty("AlarmDelayTime", new TimeSpan(0, 0, 0, 10)); }
            set { SetProperty("AlarmDelayTime", value); }
        }

        [XmlElement]
        [Browsable(false)]
        [Category("Detection && Record")]
        public string AlarmDelayTimeXml
        {
            get { return XmlConvert.ToString(AlarmDelayTime); }
            set { AlarmDelayTime = XmlConvert.ToTimeSpan(value); }
        }

        [XmlElement]
        [DisplayName("Flix X-axis")]
        [Category("Video source settings")]
        [Description("Flip X-axis")]
        public bool FlipX
        {
            get
            {
                return (bool)GetProperty("FlipX", false);
            }

            set
            {
                SetProperty("FlipX", value);
            }
        }

        [XmlElement]
        [DisplayName("Flix Y-axis")]
        [Category("Video source settings")]
        [Description("Flip Y-axis")]
        public bool FlipY
        {
            get
            {
                return (bool)GetProperty("FlipY", false);
            }

            set
            {
                SetProperty("FlipY", value);
            }
        }

        [XmlElement]
        [Category("Video source settings")]
        [Description("Desired framerate. May vary depending on hardware.")]
        public int Framerate
        {
            get
            {
                return (int)GetProperty("Framerate", 5);
            }

            set
            {
                SetProperty("Framerate", value);
            }
        }

        [XmlElement]
        [Browsable(false)]
        public Guid Id
        {
            get { return (Guid)GetProperty("Id", Guid.NewGuid()); }
            set { SetProperty("Id", value); }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlIgnore]
        [DisplayName("Max. time per record")]
        [Category("Detection && Record")]
        [Description("Maximum record length. If event still occurs a new records will be started.")]
        public TimeSpan MaxRecordDuration
        {
            get { return (TimeSpan)GetProperty("MaxRecordDuration", new TimeSpan(0, 0, 0, 120)); }
            set { SetProperty("MaxRecordDuration", value); }
        }

        [XmlElement]
        [Browsable(false)]
        public string MaxRecordDurationXml
        {
            get { return XmlConvert.ToString(MaxRecordDuration); }
            set { MaxRecordDuration = XmlConvert.ToTimeSpan(value); }
        }

        [XmlElement]
        [Browsable(false)]
        public string MonikerString
        {
            get
            {
                return (string)GetProperty("MonikerString", string.Empty);
            }

            set
            {
                SetProperty("MonikerString", value);
            }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        [DisplayName("Enabled")]
        [Category("Detection && Record")]
        [Description("Switch on and off recording.")]
        public bool MotionDetectionEnabled
        {
            get { return (bool)GetProperty("MotionDetectionEnabled", true); }
            set { SetProperty("MotionDetectionEnabled", value); }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        [Browsable(false)]
        public Rectangle[] MotionZones
        {
            get { return (Rectangle[])GetProperty("MotionZones", null); }
            set { SetProperty("MotionZones", value); }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        [DisplayName("Name")]
        [Description("Name of the surveilance.")]
        public string Name
        {
            get { return (string)GetProperty("Name", "unknown"); }
            set { SetProperty("Name", value); }
        }

        [XmlIgnore]
        [DisplayName("Prefetch time")]
        [Category("Detection && Record")]
        [Description("Time to be recorded before event occurs.")]
        public TimeSpan PrefetchTime
        {
            get { return (TimeSpan)GetProperty("PrefetchTime", new TimeSpan(0, 0, 0, 10)); }
            set { SetProperty("PrefetchTime", value); }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        [Browsable(false)]
        public string PrefetchTimeXml
        {
            get { return XmlConvert.ToString(PrefetchTime); }
            set { PrefetchTime = XmlConvert.ToTimeSpan(value); }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        [DisplayName("Record directory")]
        [Category("Detection && Record")]
        [Description("Directory to save records")]
        public string RecordBaseDirectory
        {
            get { return (string)GetProperty("RecordBaseDirectory", string.Empty); }
            set { SetProperty("RecordBaseDirectory", value); }
        }

        [XmlElement]
        [DisplayName("Render timestamp")]
        [Category("Video source settings")]
        [Description("Render timestamp to live video picture and recording")]
        public bool RenderTimestamp
        {
            get
            {
                return (bool)GetProperty("RenderTimestamp", false);
            }

            set
            {
                SetProperty("RenderTimestamp", value);
            }
        }

        [XmlElement]
        [DisplayName("Rotation")]
        [Category("Video source settings")]
        [Description("Rotate angle")]
        public int RotateAngle
        {
            get
            {
                return (int)GetProperty("RotateAngle", 0);
            }

            set
            {
                SetProperty("RotateAngle", value);
            }
        }

        [XmlElement]
        [DisplayName("Record blind time (sec)")]
        [Category("Detection && Record")]
        [Description("Seconds after startup that recording is disabled. May be usefull on activation jitter of cameras.")]
        public int StartupBlindTime
        {
            get
            {
                return (int)GetProperty("StartupBlindTime", 10);
            }

            set
            {
                SetProperty("StartupBlindTime", value);
            }
        }

        /// <summary>
        ///
        /// </summary>
        [XmlElement]
        [DisplayName("Threshold")]
        [Category("Detection && Record")]
        [Description("Threshold level to start recording.")]
        public int Threshold
        {
            get { return (int)GetProperty("Threshold", 100); }
            set
            {
                if ((value < 0) || (value > 100))
                    return;
                SetProperty("Threshold", value);
            }
        }

        [XmlElement]
        [Category("Video source settings")]
        [Description("Name of the video source")]
        [ReadOnly(true)]
        public string VideoSourceName
        {
            get
            {
                return (string)GetProperty("VideoSourceName", string.Empty);
            }

            set
            {
                SetProperty("VideoSourceName", value);
            }
        }

        public override string ToString()
        {
            return string.Format("'{0}', hardware '{1}'", Name, VideoSourceName);
        }
    }
}